import ModuleTemplate from '@/utils/ModuleTemplate';
import systemConfig from '@/utils/config';
import appService from '@/utils/appService';
/** 交易资金对账 */
export const namespace = 'accountChecking';
const model = {
  name: namespace,
  /** 基本属性 */
  state: {
    /** 分页数据查询条件 */
    query: {
      currentPage: 1,
      pageSize: 10,
      checkCode: '', // 对账编号
      different_types: [1, 2, 3] // 差异类型
    }
  },
  /** 从基本数据派生的数据,getters接收state作为其第一个参数，
   * 接受其他 getters 作为第二个参数，如不需要，第二个参数可以省略
   */
  getters: {},
  /** 执行同步操作，包含用于直接修改state的函数 */
  mutations: {
    /** 如果 url 没有带参数就重置一下 query 的参数 **/
    reviseQuery(store) {
      store.query = {
        currentPage: 1,
        pageSize: 10,
        checkCode: '', // 对账编号
        different_types: [1, 2, 3] // 差异类型
      };
    }
  },
  /** 其他异步接口 */
  actions: {
    // 商户信息列表
    getMerchandList(store, payload) {
      return new Promise((resolve, reject) => {
        appService
          .getRequest(
            `${config.tradeDataSource}/trade-merchant/admin/merchant/findOnlyMerchantList`,
            payload
          )
          .then(result => {
            if (result && result.status === 200) {
              resolve(result);
            } else {
              reject(Error);
            }
          });
      });
    },
    querySettleSummaryOne(store, payload) {
      return new Promise((resolve, reject) => {
        appService
          .postRequest(
            `${config.dataSource}/admin/settleSummary/querySettleSummaryOne`,
            payload
          )
          .then(result => {
            if (result && result.status === 200) {
              resolve(result);
            } else {
              reject(Error);
            }
          });
      });
    },
    querySettleException(store, payload) {
      return new Promise((resolve, reject) => {
        appService
          .getRequest(
            `${config.dataSource}/admin/settleDetail/settleException/${payload.checkCode}`,
            {}
          )
          .then(result => {
            if (result && result.status === 200) {
              resolve(result);
            } else {
              reject(Error);
            }
          });
      });
    },
    querySettleExceptionList(store, payload) {
      return new Promise((resolve, reject) => {
        appService
          .postRequest(`${config.dataSource}/admin/settleDetail/list`, payload)
          .then(result => {
            if (result && result.status === 200) {
              resolve(result);
            } else {
              reject(Error);
            }
          });
      });
    },
    // 导出excel
    exportOne(store, payload) {
      return new Promise((resolve, reject) => {
        appService
          .postRequest(
            `${config.dataSource}/admin/settleSummary/exportOne`,
            payload,
            { responseType: 'arraybuffer' }
          )
          .then(result => {
            if (result && result.status === 200) {
              resolve(result);
            } else {
              reject(Error);
            }
          });
      });
    },
    exportAll(store, payload) {
      return new Promise((resolve, reject) => {
        appService
          .postRequest(
            `${config.dataSource}/admin/settleSummary/exportAll`,
            payload,
            { responseType: 'arraybuffer' }
          )
          .then(result => {
            if (result && result.status === 200) {
              resolve(result);
            } else {
              reject(Error);
            }
          });
      });
    },
    /** 获取差异原因 **/
    getDiffCauseList(store, payload) {
      return new Promise((resolve, reject) => {
        appService
          .getRequest(
            `${config.dataSource}/admin/property/list?module=settlement_diff_config`,
            payload
          )
          .then(result => {
            if (result.data.ret === 0) {
              resolve(result);
            } else {
              reject(result.data.msg);
            }
          });
      });
    },

    /** 提交差异备注 **/
    submitDiffCause(store, payload) {
      return new Promise((resolve, reject) => {
        appService
          .postRequest(
            `${config.dataSource}/admin/settleSummary/markBatch`,
            payload
          )
          .then(result => {
            if (result.data.ret === 0) {
              resolve(result);
            } else {
              reject(result.data.msg);
            }
          });
      });
    },

    /** 更新对账 **/
    doReSettle(store, payload) {
      return new Promise((resolve, reject) => {
        appService
          .putRequest(`${config.dataSource}/admin/settleSummary/`, payload)
          .then(result => {
            if (result.data.ret === 0) {
              resolve(result);
            } else {
              reject(result.data.msg);
            }
          });
      });
    }
  }
};
const config = {
  /** 数据源，对应后端controller类的请求映射,
   * 如果不指定则默认和name一样
   */
  tradeDataSource: systemConfig.serverProxy,
  dataSource: systemConfig.serverProxy + '/trade-settlement',
  // dataSource: systemConfig.tradeInvoicePath,
  invoiceServer: true,
  /** query函数调用的后端接口path */
  queryMethod: '/admin/settleDetail/list',
  updateMethod: null,
  deleteMethod: null,
  saveMethod: null,
  getDetailMethod: null
};
const moduleTemplate = new ModuleTemplate(model, config);
export const module = moduleTemplate.createModel();
